﻿using System.Windows;
using System.Windows.Controls;
using BrainTechLLC;
using System.Windows.Data;
using BrainTechLLC.WPF;

namespace EventWindowHandler.Plugin
{
    /// <summary>
    /// Interaction logic for RecordedEventInfoView.xaml
    /// </summary>
    public partial class RecordedEventInfoView : UserControl
    {
        protected SimpleDispatcherTimer Timer;

        public RecordedEventInfoView()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(RecordedEventInfoView_Loaded);
        }

        void RecordedEventInfoView_Loaded(object sender, RoutedEventArgs e)
        {
            Binding b = new Binding("ShowDebug")
            {
                Source = DebugSettings.DebugSettingsInstance,
                Mode = BindingMode.OneWay,
            };
            mnuTraceEvents.SetBinding(MenuItem.IsCheckedProperty, b);

            Timer = new SimpleDispatcherTimer(System.Windows.Threading.DispatcherPriority.Background, 3500, timer =>
            {
                if (RefreshList())
                {
                    return RunInitializationResult.DidSomething;
                }
                else
                {                    
                    return RunInitializationResult.DidntDoAnything;
                }
            });

            Timer.Start();
        }

        public static readonly DependencyProperty RecordedEventInfoViewModelProperty =
            DependencyProperty.Register("RecordedEventInfoViewModel", typeof(RecordedEventInfoViewModel), typeof(RecordedEventInfoView), new PropertyMetadata(null, OnRecordedEventInfoViewModelPropertyChanged));

        public RecordedEventInfoViewModel RecordedEventInfoViewModel
        {
            get { return (RecordedEventInfoViewModel)GetValue(RecordedEventInfoViewModelProperty); }
            set { SetValue(RecordedEventInfoViewModelProperty, value); }
        }

        private static void OnRecordedEventInfoViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RecordedEventInfoView source = d as RecordedEventInfoView;
            source.UpdateRecordedEventInfoViewModel(e.NewValue as RecordedEventInfoViewModel, e.OldValue as RecordedEventInfoViewModel);
        }

        public void UpdateRecordedEventInfoViewModel(RecordedEventInfoViewModel viewModel, RecordedEventInfoViewModel viewModelOld)
        {
            RecordedEventInfoViewModel = viewModel;
            this.DataContext = RecordedEventInfoViewModel;
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            RefreshList();
        }

        public bool RefreshList()
        {
            return RecordedEventInfoViewModel.ReconcileListDifferences(RecordedEventInfo.Instance.EventInfos.AllItems);
        }

        private void mnuTraceEvents_Checked(object sender, RoutedEventArgs e)
        {
            HandleMenuTraceEventsCheckUncheck();
        }

        private void HandleMenuTraceEventsCheckUncheck()
        {
            if (mnuTraceEvents.IsChecked)
            {
                DebugSettings.DebugSettingsInstance.ShowDebug = true;
            }
            else
            {
                DebugSettings.DebugSettingsInstance.ShowDebug = false;
            }
        }

        private void mnuTraceEvents_Unchecked(object sender, RoutedEventArgs e)
        {
            HandleMenuTraceEventsCheckUncheck();
        }

        private void mnuClear_Click(object sender, RoutedEventArgs e)
        {
            RecordedEventInfo.Instance.EventInfos.Clear();
            RefreshList();
        }
    }
}
